import matplotlib.pyplot as plt
import numpy as np
import math
#t_0 = np.arange(0, 4, 0.1)
T = 1
x_measure = [0] * 4
#x_hat = [0]*len(t_0)
x_hat = [0]*4
K = list(range(1,len(x_measure)+1))
#print(K)
t = [0] * len(K)
a = [[0], [0]]
print('请输入：')
for i in range(0, 4):
    a = input()
    a = float(a)
    x_measure[i] = a
    t[i] = (K[i] - 1) * T

m1 = np.array([[4, sum(t), sum(list(map(lambda x:x**2, t)))],
               [sum(t), sum(list(map(lambda x:x**2, t))),sum(list(map(lambda x:x**3, t)))],
               [sum(list(map(lambda x:x**2, t))),sum(list(map(lambda x:x**3, t))), sum(list(map(lambda x:x**4, t)))]])

w = [t * x_measure for t, x_measure in zip(t, x_measure)]
y = list(map(lambda x:x**2, t))
z = [x_measure * y for x_measure, y in zip(x_measure, y)]
m2 = np.array([[sum(x_measure)], [sum(w)], [sum(z)]])
a = (np.linalg.inv(m1)) . dot(m2)

a0 = float(a[0])
a1 = float(a[1])
a2 = float(a[2])

for i in range(0,4):#for i in range(0,len(t_0))
    #x_hat[i] = a0 + a1 * t_0[i] + a2 * (t_0[i] ** 2)
    x_hat[i] = a0 + a1 * t[i] + a2 * (t[i] ** 2)
plt.xlabel('Time(sec)')
plt.ylabel(x_hat and x_measure)
plt.plot(t, x_measure,'+')
#plt.plot(t_0, x_hat)
plt.plot(t, x_hat)
plt.show()

